/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#ifndef CES_LOADER_HH_
#define CES_LOADER_HH_

#include "carina/scene.hh"
#include "carina/attribute.hh"
#include "carina/resources.hh"
#include "carina/node-manager.hh"
#include "carina/world-manager.hh"
#include "carina/common/ces-file.hh"

namespace Carina
{
class CESLoader
{
public:
    static bool loadResources(CESFile& ces_file, ResourceManager& res_mgr, const WorldManagerPtr& wmgr);
    static bool loadGeometry(CESGeometrySection& ces_geom, ResourceManager& res_mgr);
    static bool loadEntities(CESEntitySection& ces_ent, CESGeometrySection& ces_geom, ResourceManager& res_mgr, const WorldManagerPtr& wmgr);
    static ModelPtr loadModel(CESEntity& ces_ent, CESGeometrySection& ces_geom, ResourceManager& res_mgr);
    static NodePtr loadNode(CESNode& node, const NodeManagerPtr& nmgr);
    static GlobalLightSourcePtr loadGlobalLightSource(CESAttributeContainer& ces_attr, const LightingManagerPtr& lmgr);

    static bool transferCESToAttributes(CESAttributeContainer& ces_attr, Configurable& conf);
    static bool transferAttributesToCES(Configurable& conf, CESAttributeContainer& ces_attr);

    static bool load(const string& filename, ResourceManager& res_mgr, const LightingManagerPtr& lmgr, const NodeManagerPtr& nmgr, const WorldManagerPtr& wmgr, const ScenePtr& scene);
};

class CEMLoader: public MeshLoaderBase
{
public:
    virtual MeshPtr load(const string& filename, ResourceManager& res_mgr);

    virtual void loadTriangles(const string& filename, DynamicArray<float>& vertices, DynamicArray<uint32>& indices);

    static void loadTriangles(const CEMMesh& cem_mesh, DynamicArray<float>& vertices, DynamicArray<uint32>& indices);

    static MeshPtr loadMesh(const string& filename, ResourceManager& res_mgr);
    static void saveMesh(const MeshPtr& mesh, const string& filename);

    static MeshPtr loadMesh(CEMMesh& mesh, ResourceManager& res_mgr);
};
}

#endif /* CES_LOADER_HH_ */